package com.whfc.ms.api.cfg.service.impl;

import com.alibaba.fastjson.JSON;
import com.whfc.common.exception.BizException;
import com.whfc.common.param.ConfigCenterParam;
import com.whfc.common.result.ResultEnum;
import com.whfc.ms.api.cfg.service.MsConfigCenterService;
import com.whfc.ms.api.cfg.service.factory.ConfigCenterService;
import com.whfc.ms.api.cfg.service.factory.ConfigCenterServiceFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-07-30 11:38
 */
@Service
public class MsConfigCenterServiceImpl implements MsConfigCenterService {

    @Autowired
    private ConfigCenterServiceFactory configCenterServiceFactory;

    @Override
    public List getConfigList(Integer deptId, String module) {
        return configCenterServiceFactory.getConfigCenterService(module).getConfigList(deptId, module);
    }

    @Override
    public void setConfig(ConfigCenterParam param) {
        String module = param.getModule();
        ConfigCenterService configCenterService = configCenterServiceFactory.getConfigCenterService(module);
        if (configCenterService == null) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "未找到指定模块服务");
        }
        String dataStr = JSON.toJSONString(param.getData());
        configCenterService.setConfig(param.getDeptId(), module, dataStr);
    }
}
